package com.xing.binbackend.pattern.strategy33.ecommerce.pay;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;

public class StrategyFactory {
    // 策略注册使用Map结构 它这样写是因为多选1 我那里用的是List
    private static final Map<String, Function<Map<String, Object>, PaymentStrategy>> STRATEGY_MAP = new HashMap<>();

    static {
        // 注册策略创建逻辑
        STRATEGY_MAP.put("alipay", params -> new AlipayStrategy());
        STRATEGY_MAP.put("wechat", params -> {
            // 示例：带参数的策略创建
            Boolean useSandbox = (Boolean) params.getOrDefault("useSandbox", false);
            return new WechatPayStrategy(useSandbox);
        });
    }

    public static PaymentStrategy getStrategy(String type, Map<String, Object> params) {
        Function<Map<String, Object>, PaymentStrategy> creator = STRATEGY_MAP.get(type);
        if (creator == null) {
            throw new IllegalArgumentException("Unsupported payment type: " + type);
        }
        return creator.apply(params);
    }
}
